package com.ffcs.web.utils;

import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;  


/**
 * @ClassName: RestHttpsUtil 
 * @Description: rest https 工具类 （ssl方式）
 * @Copyright: Copyright (c) 2010 FFCS All Rights Reserved
 * @Company 北京福富软件福州分公司
 * @author 何传琛
 * @date 2014-9-1 下午04:04:45
 * @version 1.00.00
 * @history:
 */
@SuppressWarnings("all")
public class RestHttpsUtil {

	public static String Method_GET = "GET";
	public static String Method_POST = "POST";
	public static String Method_PUT = "PUT";
	public static String Method_DELETE = "DELETE";

	/**
	 * Send SSL Request
	 * 
	 * @param reqURL
	 * @param token
	 * @param str
	 * @return
	 */
	@SuppressWarnings("finally")
	public static String sendSSLRequest(String reqURL, String token, String body, String method) throws Exception {

		String responseContent = null;
		HttpClient httpClient = new DefaultHttpClient();

		//公司代理的设置
	    //HttpHost proxy = new HttpHost("192.168.13.19", 7777);
	    //httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);  
	    
		
		X509TrustManager xtm = new X509TrustManager() {
			public void checkClientTrusted(X509Certificate[] chain, String authType)
					throws CertificateException {
			}

			public void checkServerTrusted(X509Certificate[] chain, String authType)
					throws CertificateException {
			}

			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		};

		try {
			SSLContext ctx = SSLContext.getInstance("TLS");

			ctx.init(null, new TrustManager[] { xtm }, null);
			SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
			httpClient.getConnectionManager().getSchemeRegistry()
					.register(new Scheme("https", 443, socketFactory));
			HttpResponse response = null;

			if (method.equals(Method_POST)) {
				HttpPost httpPost = new HttpPost(reqURL);
				httpPost.setEntity(new StringEntity(body, "UTF-8"));
				if (token != null) {
					httpPost.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpPost);
			} else if (method.equals(Method_PUT)) {
				HttpPut httpPut = new HttpPut(reqURL);
				httpPut.setEntity(new StringEntity(body, "UTF-8"));
				if (token != null) {
					httpPut.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpPut);
			} else if (method.equals(Method_GET)) {
				HttpGet httpGet = new HttpGet(reqURL);
				if (token != null) {
					httpGet.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpGet);
			} else if (method.equals(Method_DELETE)) {
				HttpDelete httpDelete = new HttpDelete(reqURL);
				if (token != null) {
					httpDelete.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpDelete);
			}
			HttpEntity entity = response.getEntity();

			if (null != entity) {
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return responseContent;
	}
	
	public static String sendSSLRequestByproxy(String reqURL, String token, String body, String method,String proxyIp,int proxyPort) throws Exception {

		String responseContent = null;
		HttpClient httpClient = new DefaultHttpClient();

		//httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);
		 // 代理的设置
	    //HttpHost proxy = new HttpHost("192.168.13.19", 7777);
	    HttpHost proxy = new HttpHost(proxyIp, proxyPort);
	    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);  

		// HttpClient httpClient = HttpClients.createDefault();
		X509TrustManager xtm = new X509TrustManager() {
			public void checkClientTrusted(X509Certificate[] chain, String authType)
					throws CertificateException {
			}

			public void checkServerTrusted(X509Certificate[] chain, String authType)
					throws CertificateException {
			}

			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		};

		try {
			
			SSLContext ctx = SSLContext.getInstance("TLS");

			ctx.init(null, new TrustManager[] { xtm }, null);
			SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
			httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
			
			HttpResponse response = null;


			if (method.equals(Method_POST)) {
				HttpPost httpPost = new HttpPost(reqURL);
				httpPost.setEntity(new StringEntity(body, "UTF-8"));
				if (token != null) {
					httpPost.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpPost);
			   
			} else if (method.equals(Method_PUT)) {
				HttpPut httpPut = new HttpPut(reqURL);
				httpPut.setEntity(new StringEntity(body, "UTF-8"));
				if (token != null) {
					httpPut.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpPut);
				
			} else if (method.equals(Method_GET)) {
				HttpGet httpGet = new HttpGet(reqURL);
				if (token != null) {
					httpGet.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpGet);
				
			} else if (method.equals(Method_DELETE)) {
				HttpDelete httpDelete = new HttpDelete(reqURL);
				if (token != null) {
					httpDelete.setHeader("Authorization", "Bearer " + token);
				}
				response = httpClient.execute(httpDelete);				
			}

			HttpEntity entity = response.getEntity();

			if (null != entity) {
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity);
			}
		
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			httpClient.getConnectionManager().shutdown();
		}

		return responseContent;
	}


}